﻿using AutoMapper;
using MediatR;
using S.Produce.Api.Application.Commands.BreedingCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.BreedingCommandHandlers
{
    /// <summary>
    /// 
    /// </summary>
    public class BreedingUpdateCommandHandler : IRequestHandler<BreedingUpdateCommand, int>
    {
        private readonly IRepository<Breeding> repository;
        private readonly IMapper mapper;

        /// <summary>
        /// 构造方式注入
        /// </summary>
        /// <param name="repository"></param>
        /// <param name="mapper"></param>
        public BreedingUpdateCommandHandler(IRepository<Breeding> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 修改配种信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<int> Handle(BreedingUpdateCommand request, CancellationToken cancellationToken)
        {
            var info = new Breeding();
            request.UpdateDate = DateTime.Now.Date;
            return await repository.UpdateAsync(mapper.Map<BreedingUpdateCommand, Breeding>(request, info));
        }
    }
}
